﻿/*
创建人：zhangrj
日期：2022-08-05
描述：工步检验主页面js
*/
var page = {
    rootPath: getRootPath(), //网站根路径
    url: getRootPath() + "/Ashx/ContainerStepInfoCheckHandler.ashx", //后台处理url,
    commonUrl: getRootPath() + "/Ashx/CommonHandler.ashx", //通用方法后台路径
    //设备分页参数
    pageTotal: 0,
    pageSize: 10,
    pageNumber: 1,
    init: function () {
        page.bind();
    },
    //绑定事件
    bind: function () {
        customizedEasyuiPagination('pp'); //定制化分页
        //主批次列表加载
        $("#gdContainerInfo").datagrid({
            onLoadSuccess: function (data) {
                $('#pp').pagination('refresh', {	// 改变选项，并刷新分页栏信息
                    total: page.pageTotal,
                    pageSize: page.pageSize,
                    pageNumber: page.pageNumber
                });
                customizedEasyuiPagination('pp'); //定制化分页
            },
            columns: [[
                 { field: 'ck', title: '选择', checkbox: true },
                 { field: 'MFGORDERNAME', title: '订单号', width: 120 },
                 { field: 'PRODUCTBATCH', title: '件号/批次号', width: 250 },
                 { field: 'WIPQTY', title: '在制数', width: 50 },
                 { field: 'WORKFLOWSTEPNAME', title: '工序', width: 180 },
                 { field: 'STEPINFONAME', title: '工步', width: 150 },
                 { field: 'CONTAINERTYPENAME', title: '批次类型', width: 100 },
                 { field: 'REPORTQTY', title: '已报数', width: 50 },
                 { field: 'FIRSTCHECKNUM', title: '首检', width: 50, formatter: function (value, row, index) {
                     if (value === 1) {
                         return "是";
                     }
                     else { return "否"; }
                 }
                 },
                 { field: 'STEPCHECKNUM', title: '序检', width: 50, formatter: function (value, row, index) {
                     if (value === 1) {
                         return "是";
                     }
                     else { return "否"; }
                 }
                 }
                ]],
            onSelect: function (rowIndex, rowData) {
                page.bindContainerDetail(rowData);
            }
        });
        //批次详细列表加载
        $("#gdContainerDetail").datagrid({
            columns: [[
                 { field: 'ck', title: '选择', checkbox: true },
                 { field: 'CONTAINERNAME', title: '批次号', width: 180 },
                 { field: 'QTY', title: '报工数', width: 50 },
                 { field: 'EMPLOYEENAME', title: '报工人', width: 80 },
                 { field: 'CHECKTYPE', title: '检验类型', width: 60, formatter: function (value, row, index) {
                     if (value === 1) {
                         return "首检";
                     }
                     else { return "序检"; }
                 }
                 },
                 { field: 'IsFillCheckPoint', title: '检测项录入', width: 100 },
                ]]
        });
        //主批次列表分页事件
        $('#pp').pagination({
            onSelectPage: function (pageNumber, pageSize) {
                customizedEasyuiPagination('pp'); //定制化分页
                page.clearPage(false, true, true, true);
                page.pageSize = pageSize; page.pageNumber = pageNumber;
                page.search();
            }
        });
        $(document).ready(function () {
            //绑定分页go按钮
            $('#pageGo').on("click", function () {
                page.clearPage(false, true, true, true);
                var pageNumber = $(this).parents('tr').find('.pagination-num').val();
                page.pageNumber = pageNumber;
                page.search();
            });
        });
        //查询信息
        $("#btnSearch").on("click", function () {
            page.clearPage(false, true, true, true);
            page.pageNumber = 1;
            page.search();
        });
        //重置查询
        $("#btnReset").on("click", function () {
            page.clearPage(true, true, true, true);
        });
        //检验
        $("#btnCheck").on("click", function () {
            page.clearPage(false, false, false, true);
            page.stepInfoCheck();
        });
        //实测值录入
        $("#btnInputCheckPoint").on("click", function () {
            var selected = $("#gdContainerInfo").datagrid("getChecked");
            if (selected.length === 0) {
                displayMessage("未选择数据", false);
                return false;
            }
            var selected1 = $("#gdContainerDetail").datagrid("getChecked");
            if (selected1.length == 0) {
                displayMessage("未选择数据", false);
                return false;
            }
            var main = selected1;
            //            if (selected[0].CONTAINERTYPE !== 7) {
            //                for (var i = 0; i < selected.length; i++) {
            //                    selected[i].CONTAINERID = selected1[0].CONTAINERID;
            //                }
            //                main = selected;
            //            }
            page.post(page.url, { action: "SetInputCheckPoint", mainDt: JSON.stringify(main) }, true, function (res) {
                if (!res.Result) {
                    displayMessage(res.Message, false);
                    return;
                }
                page.openStepCheckItem();
            });
        });
        //批次流水卡打印查看
        $('#btnViewContainer').on("click", function () {
            clearMessage();
            page.viewContainerPrint();
        });
        //文档查看按钮
        $('#btnViewDocument').on("click", function () {
            clearMessage();
            page.viewDoc();
        });
        //理化试验申请
        $('#btnDelegateLH').on("click", function () {
            clearMessage();
            page.delegateLH();
        });
    },
    //查询
    search: function () {
        var formData = {
            action: "GetStepInfoCheckList",
            pageSize: page.pageSize,
            pageIndex: page.pageNumber,
            scanContainer: $.trim($("#txtScanContainer").textbox("getValue")),
            scanOrder: $.trim($("#txtScanOrder").textbox("getValue")),
            productName: $.trim($("#txtProductName").textbox("getValue")),
            containerName: $.trim($("#txtContainerName").textbox("getValue")),
            stepName: $.trim($("#txtStepName").textbox("getValue")),
            combinateName: $("#txtCombinate").textbox("getValue")
        };
        page.post(page.url, formData, true, function (res) {
            if (!res.Result) {
                displayMessage(res.Message, res.Result);
                return;
            }
            page.pageTotal = res.RowCount;
            $("#gdContainerInfo").datagrid('loadData', { total: res.RowCount, rows: res.DBTable });
        });
    },
    //绑定批次详细
    bindContainerDetail: function (row) {
        var datas = [];
        datas.push(row);
        page.post(page.url, { action: "GetStepInfoDetailCheckList", params: JSON.stringify(datas) }, true, function (res) {
            if (!res.Result) {
                displayMessage(res.Message, res.Result);
            }
            $("#gdContainerDetail").datagrid('loadData', { rows: res.DBTable });
            $("#gdContainerDetail").datagrid('selectAll');
        });
    },
    //检验弹出
    stepInfoCheck: function () {
        var container = $("#gdContainerInfo").datagrid("getChecked");
        var selected = $("#gdContainerDetail").datagrid("getChecked");
        if (selected.length == 0) {
            displayMessage("未选择需要检验的报工数据", false);
            return false;
        }
        //验证批次，成功后打开弹窗
        page.post(page.url, { action: "IsValidContainer", params: JSON.stringify(selected) }, true, function (res) {
            if (!res.Result) {
                displayMessage(res.Message, res.Result);
                return;
            }
            if (res.ReturnData === "") {
                displayMessage("数据有误，没有报工数据", false);
                return;
            }
            //打开弹出页面
            var url = getRootPath() + "/forms/Custom/htmlPage/ContainerStepInfoCheckFormPage.html?v=" + new Date().getTime();
            url += "&parentDiv=dg";
            $('#dg').dialog({
                title: '检验',
                width: 1000,
                height: 600,
                top: 10,
                closed: false,
                cache: false,
                modal: true,
                content: '<iframe src="' + url + '" width="100%" height="99%" frameborder="0" scrolling="auto"></iframe>',
                poPupData: [{ container: container, reportIds: res.ReturnData}], //弹出页面的传参
                onClose: function () {
                    var returnValue = window.returnValue;
                    if (returnValue != null) {
                        if (returnValue.isSuccess == true) {
                            page.clearPage(false, true, true, true);
                            page.search();
                        }
                        displayMessage(returnValue.message, returnValue.isSuccess);
                    }
                }
            });
        });
    },
    //检测项弹出页面
    openStepCheckItem: function () {
        //打开弹出页面
        var url = getRootPath() + "/forms/Custom/InputStepCheckPointResultPopupNewForm.aspx?v=" + new Date().getTime();
        window.open(url);
        //        url += "&parentDiv=dg";
        //        $('#dg').dialog({
        //            title: '实测值录入',
        //            width: 1100,
        //            height: 800,
        //            top: 10,
        //            closed: false,
        //            cache: false,
        //            modal: true,
        //            content: '<iframe src="' + url + '" width="100%" height="99%" frameborder="0" scrolling="auto"></iframe>',
        //            // poPupData: selected, //弹出页面的传参
        //            onClose: function () {
        //                var returnValue = window.returnValue;
        //                if (returnValue != null) {
        //                    if (returnValue.isSuccess == true) {

        //                    }
        //                }
        //            }
        //        });
    },
    //预览批次打印页面 
    viewContainerPrint: function () {
        var selected = $("#gdContainerInfo").datagrid("getChecked");
        if (selected.length === 0) {
            displayMessage("未选择数据", false);
            return false;
        }
        var selected1 = $("#gdContainerDetail").datagrid("getChecked");
        if (selected1.length == 0) {
            displayMessage("未选择数据", false);
            return false;
        }
        //        var main = selected1;
        //        if (selected[0].CONTAINERTYPE !== 7) {
        //            for (var i = 0; i < selected.length; i++) {
        //                selected[i].CONTAINERID = selected1[0].CONTAINERID;
        //            }
        //            main = selected;
        //        }
        page.post(page.commonUrl, { action: "viewPrintPage", isReBiao: "1", params: JSON.stringify(selected1) }, true, function (result) {
            if (!result.Result) {
                displayMessage(result.Message, false);
                return;
            }
            var url = result.ReturnData + "?v=" + new Date().getTime();
            window.open(url);
        });
    },
    //理化试验申请
    delegateLH: function () {
        var selected = $("#gdContainerDetail").datagrid("getChecked");
        if (selected.length === 0) {
            displayMessage("未选择批次", false);
            return false;
        }
        if (selected.length > 1) {
            displayMessage("只能选择一个批次进行理化试验", false);
            return false;
        }
        //验证批次，成功后打开弹窗
        page.post(page.url, { action: "SetDelegateLHSession", containerID: selected[0].CONTAINERID, stepInfoID: selected[0].STEPINFOID }, true, function (res) {
            if (!res.Result) {
                displayMessage(res.Message, res.Result);
                return;
            }
            //打开弹出页面
            var url = getRootPath() + "/forms/Custom/EntrustMeteringPopupNewForm.aspx?v=" + new Date().getTime();
            url += "&parentDiv=dg";
            $('#dg').dialog({
                title: '理化试验',
                width: 1000,
                height: 600,
                top: 10,
                closed: false,
                cache: false,
                modal: true,
                content: '<iframe src="' + url + '" width="100%" height="99%" frameborder="0" scrolling="auto"></iframe>'
            });
        });
    },
    //文档查看
    viewDoc: function () {
        var selected = $("#gdContainerInfo").datagrid("getChecked");
        if (selected.length == 0) {
            displayMessage("请选择批次", false);
            return;
        }
        var url = page.rootPath + '/forms/Custom/' + "ProcessDocumentViewPopupForm.aspx?v=" + new Date().getTime();
        url += "&productId=" + selected[0].PRODUCTID + "&workflowId=" + selected[0].WORKFLOWID;
        $('#dg2').dialog({
            title: '文档查看',
            width: 730,
            height: 590,
            top: 10,
            closed: false,
            cache: false,
            modal: true,
            content: '<iframe src="' + url + '" width="100%" height="99%" frameborder="0" scrolling="auto"></iframe>',
            onClose: function () {

            }
        });
    },
    //清除页面（查询区域，列表区域，详细区域，是否清空消息）
    clearPage: function (query, list, detail, msg) {
        if (query) {
            $("#txtScanContainer").textbox("setValue", ""); //扫描批次号
            $("#txtScanOrder").textbox("setValue", ""); //扫描订单号
            $("#txtProductName").textbox("setValue", ""); //件号
            $("#txtContainerName").textbox("setValue", ""); //批次号
            $("#txtStepName").textbox("setValue", ""); //工序号
        }
        if (list) {
            clearEasyuiDataGrid("gdContainerInfo"); //清空列表
        }
        if (detail) {
            clearEasyuiDataGrid("gdContainerDetail"); //清空详细列表
        }
        if (msg) {
            clearMessage();
        }
    },
    //post数据（路径，参数，是否异步，成功返回方法）
    post: function (url, param, async, callback) {
        $.ajax({
            type: "Post",
            url: url,
            data: param,
            cashe: false,
            async: async,
            dataType: "json",
            beforeSend: function () {
                $.messager.progress({ text: '', msg: '处理中...' });
            },
            success: function (data) {
                $.messager.progress('close');
                callback(data);
            },
            error: function (XMLHttpRequest, textStatus, errorThrown) {
                $.messager.progress('close');
                displayMessage(XMLHttpRequest.responseText, false);
                $("body").find(".easyui-linkbutton").each(function () {
                    $(this).linkbutton('enable');
                });
            }
        });
    }
}
page.init();